/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.pp.bizon.cripto.gui;

import java.io.File;

import javax.swing.JFileChooser;

import ua.pp.bizon.cripto.file.FileFactory;
import ua.pp.bizon.cripto.file.IPath;
import ua.pp.bizon.cripto.file.IPath.Place;


/**
 *
 * @author roman
 */
public class SelectPathPanel extends javax.swing.JPanel {


    private static final long serialVersionUID = 3107349343515630071L;
    /**
     * Creates new form SelectPathPanel
     */
    public SelectPathPanel() {
        initComponents();
        buttonGroup1.add(ftpRadioButton);
        buttonGroup1.add(localRadioButton);
    }
    
    
    protected void select(IPath path){
        if (path.getPlace().equals(Place.FTP)){
            ftpRadioButton.setSelected(true);
            pathTextField.setText(path.getPath());
            loginTextField.setText(path.getUsername());
            passwordField.setText(path.getPassword());
            hostTextField.setText(path.getHost());
        } else if (path.getPlace().equals(Place.LOCALLY)){
            localRadioButton.setSelected(true);
            localTextField.setText(path.getPath());
        }
    }

    protected IPath getSelected(){
        if (ftpRadioButton.isSelected()){
            return FileFactory.getRemoteFile(hostTextField.getText(), 
                    loginTextField.getText(), 
                    new String(passwordField.getPassword()),
                    pathTextField.getText());
        } else {
            return FileFactory.getLocalFile(localTextField.getText());
        }
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        selectPethLocallyButton = new javax.swing.JButton();
        localTextField = new javax.swing.JTextField();
        ftpRadioButton = new javax.swing.JRadioButton();
        pathTextField = new javax.swing.JTextField();
        ftpUrlButton = new javax.swing.JButton();
        localRadioButton = new javax.swing.JRadioButton();
        loginTextField = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();
        hostTextField = new javax.swing.JTextField();

        selectPethLocallyButton.setText("select path");
        selectPethLocallyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectPethLocallyButtonActionPerformed(evt);
            }
        });

        localTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                localTextFieldActionPerformed(evt);
            }
        });

        ftpRadioButton.setText("ftp");
        ftpRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ftpRadioButtonActionPerformed(evt);
            }
        });

        pathTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pathTextFieldActionPerformed(evt);
            }
        });

        ftpUrlButton.setText("check url");
        ftpUrlButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ftpUrlButtonActionPerformed(evt);
            }
        });

        localRadioButton.setText("local");
        localRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                localRadioButtonActionPerformed(evt);
            }
        });

        hostTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hostTextFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(localRadioButton)
                    .addComponent(ftpRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(loginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(hostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pathTextField))
                    .addComponent(localTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(selectPethLocallyButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ftpUrlButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(17, 17, 17))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(localRadioButton)
                    .addComponent(localTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectPethLocallyButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ftpRadioButton)
                    .addComponent(pathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftpUrlButton)
                    .addComponent(loginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ftpUrlButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ftpUrlButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ftpUrlButtonActionPerformed

    private void pathTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pathTextFieldActionPerformed

    private void selectPethLocallyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectPethLocallyButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        //chooser.setVisible(true);
        int ret = chooser.showOpenDialog(this);
        if (ret == JFileChooser.APPROVE_OPTION){
            File f = chooser.getSelectedFile();
            localTextField.setText(f.getAbsolutePath());
            localRadioButton.setSelected(true);
        }
    }//GEN-LAST:event_selectPethLocallyButtonActionPerformed
    
    

    private void localRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_localRadioButtonActionPerformed

    }//GEN-LAST:event_localRadioButtonActionPerformed

    private void ftpRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ftpRadioButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ftpRadioButtonActionPerformed

    private void localTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_localTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_localTextFieldActionPerformed

    private void hostTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hostTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_hostTextFieldActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton ftpRadioButton;
    private javax.swing.JButton ftpUrlButton;
    private javax.swing.JTextField hostTextField;
    private javax.swing.JRadioButton localRadioButton;
    private javax.swing.JTextField localTextField;
    private javax.swing.JTextField loginTextField;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JTextField pathTextField;
    private javax.swing.JButton selectPethLocallyButton;
    // End of variables declaration//GEN-END:variables
}
